package cn.forward.service.impl;

import cn.dev33.satoken.secure.BCrypt;
import cn.forward.constants.Constants;
import cn.forward.constants.GlobalExceptionCode;
import cn.forward.exception.GlobalException;
import cn.forward.feign.AppFeignClient;
import cn.forward.mapper.DriverMapper;
import cn.forward.param.AppLoginParam;
import cn.forward.pojo.domain.Driver;
import cn.forward.pojo.domain.DriverSetting;
import cn.forward.pojo.domain.DriverSummary;
import cn.forward.pojo.domain.DriverWallet;
import cn.forward.pojo.domain.dto.CodToOpenId;
import cn.forward.properties.AppSettingProperties;
import cn.forward.result.R;
import cn.forward.service.IDriverService;
import cn.forward.service.IDriverSettingService;
import cn.forward.service.IDriverSummaryService;
import cn.forward.service.IDriverWalletService;
import cn.forward.template.WechatTemplate;
import cn.forward.utils.JsonToClassUtil;
import cn.forward.utils.NameUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import java.math.BigDecimal;
import java.util.Date;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigurationPackage;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RestController;



/**
 * <p>
 * 司机对象 服务实现类
 * </p>
 *
 * @author forward
 * @since 2025-03-21
 */
@Service
@Transactional
public class DriverServiceImpl extends ServiceImpl<DriverMapper, Driver> implements IDriverService {

    @Autowired
    private IDriverSummaryService summaryService;

    @Autowired
    private IDriverWalletService walletService;

    @Autowired
    private IDriverSettingService settingService;

    @Autowired
    private AppSettingProperties appSettingProperties;

    @Autowired
    private WechatTemplate wechatTemplate;

    @Autowired
    private AppFeignClient appFeignClient;


    @Override
    public void register(String openIdCod) {


        String openId = wechatTemplate.openIdByCode(openIdCod);


        Driver driver = super.getOne(new LambdaQueryWrapper<Driver>().eq(Driver::getOpenId, openId));

        if (ObjectUtil.isNotNull(driver)){
            throw new GlobalException(GlobalExceptionCode.PARAM_PHONE_EXIST);
        }

        String password = BCrypt.hashpw("123456", BCrypt.gensalt());

        driver = new Driver();

        String user = "14525418";

        saveDriver(driver,openId,user);

        Long id = driver.getId();

        saveSettingSW(id,user);
        //最后调用feign
        saveLogin(openId, user,password);



    }

    public void saveLogin(String openId, String user,String password) {
        AppLoginParam appLoginParam = new AppLoginParam();
        appLoginParam.setUsername(user);
        appLoginParam.setPassword(password);
        appLoginParam.setType(Constants.Login.TYPE_DRIVER);
        appLoginParam.setEnabled(true);
        appLoginParam.setAvatar(Constants.url.AVATAR);
        appLoginParam.setAdmin(false);
        appLoginParam.setNickName(NameUtil.getName());
        appLoginParam.setOpenId(openId);
        appLoginParam.setName(NameUtil.getName());
        appLoginParam.setPhone(user);


        R<Void> R = appFeignClient.sava(appLoginParam);
        if (! R.isSuccess()){
            throw new GlobalException(GlobalExceptionCode.WECHAT_REGISTER_ERROR);
        }

    }

    /**
     * 根据phoneCode获取手机号
     * @param phoneCod
     * @return
     */
    public String phoneCodToPhone(String phoneCod) {


        String accessToken = wechatTemplate.ToAccessToken();

        String phoneNumber = wechatTemplate.phoneByCode(phoneCod);
        return phoneNumber;

    }

    /**
     * 初始化从表
     * @param id
     */

    public void saveSettingSW(Long id,String user) {

        DriverSetting driverSetting = new DriverSetting();
        driverSetting.setId(id);
        driverSetting.setAutoAccept(false);
        driverSetting.setOrientation(false);
        driverSetting.setListenService(false);
        driverSetting.setOrderDistance(appSettingProperties.getOrderDistance());
        driverSetting.setRangeDistance(appSettingProperties.getRangeDistance());
        driverSetting.setCreateTime(new Date());
        driverSetting.setDeleted(false);
        driverSetting.setVersion(0);

        settingService.save(driverSetting);

        DriverSummary driverSummary = new DriverSummary();

        driverSummary.setId(id);
        driverSummary.setLevel(0);
        driverSummary.setTotalOrders(0);
        driverSummary.setWeekOrders(0);
        driverSummary.setGoodComments(0);
        driverSummary.setAppeal(0);
        driverSummary.setTotalComplaint(0);
        driverSummary.setTodayTotalCancel(0);
        driverSummary.setDriveDuration(0);
        driverSummary.setTradeOrders(0);
        driverSummary.setTodayTradeOrders(0);
        driverSummary.setTodayComplaint(0);
        driverSummary.setTodayCancel(0);
        driverSummary.setTodayIncome(new BigDecimal("0"));
        driverSummary.setCreateTime(new Date());
        driverSummary.setDeleted(false);
        driverSummary.setVersion(0);

        summaryService.save(driverSummary);



        DriverWallet driverWallet = new DriverWallet();
        driverWallet.setId(id);
        driverWallet.setAmount(new BigDecimal("0"));
        driverWallet.setPassword(user);
        driverWallet.setDeleted(false);
        driverWallet.setVersion(0);
        driverWallet.setCreateTime(new Date());
        walletService.save(driverWallet);
    }


    /**
     * 初始化司机driver表
     * @param driver
     * @param openid
     */

    public void saveDriver(Driver driver,String openid,String user) {

        driver.setPhone(user);
        driver.setHomeAddress("");
        driver.setHomeAddressLongitude("666");
        driver.setHomeAddressLatitude("999");
        driver.setArchive(false);
        driver.setCreateTime(new Date());
        driver.setDeleted(false);
        driver.setVersion(0);
        driver.setOpenId(openid);
        driver.setBitState(0L);
        super.save(driver);
    }
}
